package cn.wolfcode.web.controller;

import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    //字典明细页面的增删改查

    //查
    @RequirePermission(name = "字典明细页面",expression = "systemDictionaryItem:list")
    @RequestMapping("/list")
    public String query(@ModelAttribute("qo") SystemDictionaryItemQueryObject qo, Model model){
        //字典目录显示
        List<SystemDictionary> dictionaries = systemDictionaryService.selectAll();
        model.addAttribute("dictionaries",dictionaries);

        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        //返回的JSP视图
        return "systemDictionaryItem/list";
    }

    //删
    @RequirePermission(name = "字典明细删除",expression = "systemDictionaryItem:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        systemDictionaryItemService.delete(id);
        //重定向会原来的list页面
        return "redirect:/systemDictionaryItem/list";
    }

    //增改
    @RequirePermission(name = "字典明细新增/编辑",expression = "systemDictionaryItem:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem){
        systemDictionaryItemService.saveOrUpdate(systemDictionaryItem);
        return new JsonResult();
    }

    @RequestMapping("/parentSelect")
    @ResponseBody
    public List<SystemDictionaryItem> parentSelect(Long id){
        return systemDictionaryItemService.selectByParentId(id);
    }

    //根据目录的sn查对应明细
    @RequestMapping("/selectByTypeSn")
    @ResponseBody
    public List<SystemDictionaryItem> selectByTypeSn(String sn){
        return systemDictionaryItemService.selectByTypeSn(sn);
    }

    @RequestMapping("/selectByParentId")
    @ResponseBody
    public List<SystemDictionaryItem> selectByParentId(Long id){
        return systemDictionaryItemService.selectParentId(id);
    }
}
